<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家id" prop="shopId">
        <el-input
          v-model="queryParams.shopId"
          placeholder="请输入商家id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applyTime">
        <el-date-picker clearable
          v-model="queryParams.applyTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="售后原因" prop="afterSalesReason">
        <el-input
          v-model="queryParams.afterSalesReason"
          placeholder="请输入售后原因"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundPrice">
        <el-input
          v-model="queryParams.refundPrice"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker clearable
          v-model="queryParams.refundTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择退款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款账户 微信账户或者平台积分" prop="refundAccount">
        <el-input
          v-model="queryParams.refundAccount"
          placeholder="请输入退款账户 微信账户或者平台积分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后进度" prop="dealPace">
        <el-input
          v-model="queryParams.dealPace"
          placeholder="请输入售后进度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理结果" prop="dealResult">
        <el-input
          v-model="queryParams.dealResult"
          placeholder="请输入处理结果"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['order:afterSales:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:afterSales:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:afterSales:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['order:afterSales:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afterSalesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="售后单id" align="center" prop="afterSalesId" />
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="商家id" align="center" prop="shopId" />
      <el-table-column label="售后类型：1-未支付订单 2-已支付订单" align="center" prop="afterSalesType" />
      <el-table-column label="退款类型，1:-全部退款，2-部分退款" align="center" prop="refundType" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="售后原因" align="center" prop="afterSalesReason" />
      <el-table-column label="退款原因详细描述" align="center" prop="refundReasonDescription" />
      <el-table-column label="凭证照片" align="center" prop="evidenceImages" />
      <el-table-column label="退款金额" align="center" prop="refundPrice" />
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款账户 微信账户或者平台积分" align="center" prop="refundAccount" />
      <el-table-column label="售后进度" align="center" prop="dealPace" />
      <el-table-column label="处理结果" align="center" prop="dealResult" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:afterSales:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:afterSales:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改售后表管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="afterSalesRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="商家id" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入商家id" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="售后原因" prop="afterSalesReason">
          <el-input v-model="form.afterSalesReason" placeholder="请输入售后原因" />
        </el-form-item>
        <el-form-item label="退款原因详细描述" prop="refundReasonDescription">
          <el-input v-model="form.refundReasonDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="凭证照片" prop="evidenceImages">
          <el-input v-model="form.evidenceImages" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundPrice">
          <el-input v-model="form.refundPrice" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker clearable
            v-model="form.refundTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择退款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款账户 微信账户或者平台积分" prop="refundAccount">
          <el-input v-model="form.refundAccount" placeholder="请输入退款账户 微信账户或者平台积分" />
        </el-form-item>
        <el-form-item label="售后进度" prop="dealPace">
          <el-input v-model="form.dealPace" placeholder="请输入售后进度" />
        </el-form-item>
        <el-form-item label="处理结果" prop="dealResult">
          <el-input v-model="form.dealResult" placeholder="请输入处理结果" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AfterSales">
import { listAfterSales, getAfterSales, delAfterSales, addAfterSales, updateAfterSales } from "@/api/order/afterSales"

const { proxy } = getCurrentInstance()

const afterSalesList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    shopId: null,
    afterSalesType: null,
    refundType: null,
    applyTime: null,
    afterSalesReason: null,
    refundReasonDescription: null,
    evidenceImages: null,
    refundPrice: null,
    refundTime: null,
    refundAccount: null,
    dealPace: null,
    dealResult: null
  },
  rules: {
    orderId: [
      { required: true, message: "订单id不能为空", trigger: "blur" }
    ],
    shopId: [
      { required: true, message: "商家id不能为空", trigger: "blur" }
    ],
    afterSalesType: [
      { required: true, message: "售后类型：1-未支付订单 2-已支付订单不能为空", trigger: "change" }
    ],
    refundType: [
      { required: true, message: "退款类型，1:-全部退款，2-部分退款不能为空", trigger: "change" }
    ],
    afterSalesReason: [
      { required: true, message: "售后原因不能为空", trigger: "blur" }
    ],
    refundPrice: [
      { required: true, message: "退款金额不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询售后表管理列表 */
function getList() {
  loading.value = true
  listAfterSales(queryParams.value).then(response => {
    afterSalesList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    afterSalesId: null,
    orderId: null,
    shopId: null,
    afterSalesType: null,
    refundType: null,
    applyTime: null,
    afterSalesReason: null,
    refundReasonDescription: null,
    evidenceImages: null,
    refundPrice: null,
    refundTime: null,
    refundAccount: null,
    dealPace: null,
    dealResult: null
  }
  proxy.resetForm("afterSalesRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.afterSalesId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加售后表管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _afterSalesId = row.afterSalesId || ids.value
  getAfterSales(_afterSalesId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改售后表管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["afterSalesRef"].validate(valid => {
    if (valid) {
      if (form.value.afterSalesId != null) {
        updateAfterSales(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addAfterSales(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _afterSalesIds = row.afterSalesId || ids.value
  proxy.$modal.confirm('是否确认删除售后表管理编号为"' + _afterSalesIds + '"的数据项？').then(function() {
    return delAfterSales(_afterSalesIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/afterSales/export', {
    ...queryParams.value
  }, `afterSales_${new Date().getTime()}.xlsx`)
}

getList()
</script>
